#include <iplqimage.h>

IplQImage::IplQImage(const IplImage *img, int channels, QObject *parents):
        QObject(parents)
{
    m_IplImage = cvCreateImageHeader(cvSize(img->width, img->height), IPL_DEPTH_8U, 3);
    m_ImageWidth = img->width;
    m_ImageHeight = img->height;
    m_ImageChannels = channels;

    m_QImage = new QImage(QSize(m_ImageWidth, m_ImageHeight), QImage::Format_RGB888);

    m_IplImage->imageData = (char *)m_QImage->bits();

    if(channels == 3)
    {
        cvCopy(img, m_IplImage, 0);
    }
    else if(channels == 1)
    {
        cvCvtColor(img, m_IplImage, CV_GRAY2RGB);
    }


}

void IplQImage::updateImage(IplImage const *img)
{
    if(img->width==m_ImageWidth && img->height==m_ImageHeight)
    {
        if (1 == m_ImageChannels)
            cvCvtColor(img, m_IplImage, CV_GRAY2RGB);
        else
            cvCopy(img, m_IplImage, 0);
    }
}

IplQImage::~IplQImage()
{
    cvReleaseImage(&m_IplImage);
    delete m_QImage;
}

IplImage *IplQImage::getIpl()
{
    return m_IplImage;
}

QImage *IplQImage::getQImage()
{
    return m_QImage;
}

int IplQImage::getImageWidth()
{
    return m_ImageWidth;
}
